package com.cya.forumproject.services.impl;

import com.cya.forumproject.common.AppResult;
import com.cya.forumproject.common.ResultCode;
import com.cya.forumproject.dao.ArticleReplyMapper;
import com.cya.forumproject.exception.ApplicationException;
import com.cya.forumproject.model.ArticleReply;
import com.cya.forumproject.services.IArticleReplyService;
import com.cya.forumproject.services.IArticleService;
import com.cya.forumproject.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ：陈奕安（3048279304@qq.com）
 * @date ：Created in 2025/9/25 15:12
 * @description：
 * @modified By：
 * @version:
 */
@Service
@Slf4j
public class ArticleReplyServiceImpl implements IArticleReplyService {


    @Resource
    private ArticleReplyMapper articleReplyMapper;

    @Resource
    private IArticleService articleService;

    @Override
    public void create(ArticleReply articleReply) {
        if (articleReply==null||articleReply.getArticleId()==null||articleReply.getPostUserId()==null
        || StringUtil.isEmpty(articleReply.getContent())){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReply.setLikeCount(null);
        articleReply.setState((byte) 0);
        articleReply.setDeleteState((byte) 0);
        articleReply.setCreateTime(new Date());
        articleReply.setUpdateTime(new Date());

        int i = articleReplyMapper.insertSelective(articleReply);
        if (i!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        articleService.addOneReplyCountById(articleReply.getArticleId());
        log.info("帖子回复成功");
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {

        if (articleId==null||articleId<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<ArticleReply> articleReplies = articleReplyMapper.selectByArticleId(articleId);

        return articleReplies;
    }
}